//
//  TypeSingerViewController.m
//  DolphinMusic
//
//  Created by dllo on 15/3/24.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "TypeSingerViewController.h"
#import "Singer.h"
#import "SingerTableViewCell.h"
#import "SIngerViewController.h"
#import "MJRefreshFooter.h"
#import "UIScrollView+MJRefresh.h"
#import "MJRefresh.h"

@interface TypeSingerViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)MBProgressHUD *HUD;
@property (nonatomic, retain)UIView *bgView;
@property (nonatomic, retain)UITableView *singerTableView;
@property (nonatomic, retain)NSMutableArray *singerArr;
@property (nonatomic, assign)NSInteger page;
@property (nonatomic, retain)NSString *nextPageAddress;

@end

@implementation TypeSingerViewController

- (void)dealloc
{
    [_singerArr release];
    [_singerTableView release];
    [_bgView release];
    [_type release];
    [_address release];
    [_HUD release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIWindow *window = [[[UIApplication sharedApplication] windows] firstObject];
    PlayView *playView = (PlayView*)[window viewWithTag:10001];
    playView.target = self.navigationController;
    
    UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, VIEW_WIDTH, 44)];
    UIImage *image1 = [UIImage imageNamed:@"bg2.jpg"];
    image1 = [image1 resizableImageWithCapInsets:UIEdgeInsetsMake(1, 1, 1, 1)];
    imageView1.userInteractionEnabled = YES;
    imageView1.image = image1;
    [self.view addSubview:imageView1];
    [imageView1 release];
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftButton.frame = CGRectMake(0, 0, 50, 44);
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 12, 20, 20)];
    imageView.image = [UIImage imageNamed:@"backButton.png"];
    [leftButton addSubview:imageView];
    [imageView release];
    
    [leftButton addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [imageView1 addSubview:leftButton];
    

    
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 20, VIEW_WIDTH - 200, 44)];
    titleLabel.text = self.type;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont systemFontOfSize:18];
    [self.view addSubview:titleLabel];
    [titleLabel release];
    
    CGFloat ab = 70.0 / 667;
    self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, VIEW_WIDTH, VIEW_HEIGHT - 64 - ab * VIEW_HEIGHT)];
    self.bgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.bgView];
    [_bgView release];
    
    self.singerArr = [NSMutableArray array];
    
    self.HUD = [[MBProgressHUD alloc] initWithView:self.bgView];
    [self.bgView addSubview:self.HUD];
    self.HUD.labelText = @"Music is on the way...";
    self.HUD.dimBackground = YES;
    [self.HUD show:YES];
    [_HUD release];
    
    [NetHandler getDataWithUrl:self.address completion:^(id dict) {
       
        UILabel *label11 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 45)];
        label11.text = @"热门";
        [self.bgView addSubview:label11];
        [label11 release];
        
        UIView *view11 = [[UIView alloc] initWithFrame:CGRectMake(10, 45, VIEW_WIDTH - 20, 1)];
        view11.backgroundColor = [UIColor blackColor];
        view11.alpha = 0.1;
        [self.bgView addSubview:view11];
        [view11 release];
        
        
        NSArray *arr = [dict objectForKey:@"artist"];
        for (NSDictionary *dic in arr) {
            Singer *singer = [[Singer alloc] initWithDictionary:dic];
            [self.singerArr addObject:singer];
            [singer release];
        }
        
        self.page = 0;
        self.singerTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 47, VIEW_WIDTH - 10, VIEW_HEIGHT - 110) style:UITableViewStylePlain];
        self.singerTableView.delegate = self;
        self.singerTableView.dataSource = self;
        self.singerTableView.rowHeight = 90;
        [self.singerTableView registerClass:[SingerTableViewCell class] forCellReuseIdentifier:@"singerTableViewResusec"];
        self.singerTableView.showsVerticalScrollIndicator = NO;
        [self.bgView addSubview:self.singerTableView];
        [_singerTableView release];
        
        [self.singerTableView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(pullToRresh)];
        
        [self.HUD hide:YES];
    }];
    
    
}

- (void)pullToRresh
{
    
    self.nextPageAddress = [NSString stringWithFormat:@"http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.0.1&method=baidu.ting.artist.getList&format=json&offset=%ld&limit=30&order=1&area=%@&sex=%@", self.page * 30 + 60, self.area, self.sex];
    
        _page++;
    
    [NetHandler getDataWithUrl:self.nextPageAddress completion:^(id dict) {
        NSArray *arr = [dict objectForKey:@"artist"];
        
        if (arr != nil) {
            for (NSDictionary *dic in arr) {
                Singer *singer = [[Singer alloc] initWithDictionary:dic];
                [self.singerArr addObject:singer];
                [singer release];
            }
            [self.singerTableView reloadData];
            [self.singerTableView.footer endRefreshing];
        }
        else {
            self.singerTableView.footer.state = MJRefreshFooterStateNoMoreData;
        }
    }];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.singerArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SingerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"singerTableViewResusec" forIndexPath:indexPath];
    cell.singer = [self.singerArr objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    SIngerViewController *singerVC = [[SIngerViewController alloc] init];
    singerVC.singer = [self.singerArr objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:singerVC animated:YES];
    [singerVC release];
}



- (void)leftButtonAction:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    UIButton *button = (UIButton *)[window viewWithTag:1000];
    button.hidden = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
